import { getMoreHouseListData } from '@/services/modules/more'
import { paginationConfig } from '@/config'
import * as actionTypes from './constants'

export const changeCurrentPageAction = currentPage => ({
  type: actionTypes.CHANGE_CURRENT_PAGE,
  currentPage,
})
export const changeHouseListAction = houseList => ({
  type: actionTypes.CHANGE_HOUSE_LIST,
  houseList,
})
export const changeTotalCountAction = totalCount => ({
  type: actionTypes.CHANGE_TOTAL_COUNT,
  totalCount,
})
export const changeIsLoadingAction = isLoading => ({
  type: actionTypes.CHANGE_IS_LOADING,
  isLoading,
})

export const getMoreHouseListAction = (page = 0) => {
  return dispatch => {
    // 1.修改 currentPage
    dispatch(changeCurrentPageAction(page))

    // 2.更新加载状态
    dispatch(changeIsLoadingAction(true))

    // 3.获取数据
    getMoreHouseListData(page * paginationConfig.pageSize)
      .then(res => {
        const { list, totalCount } = res
        dispatch(changeHouseListAction(list))
        dispatch(changeTotalCountAction(totalCount))
      })
      .finally(() => {
        // 4.更新加载状态
        dispatch(changeIsLoadingAction(false))
      })
  }
}
